package data;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import org.w3c.dom.NodeList;


public class SimpleDOMReader extends DOMReader {
	
	public SimpleDOMReader(String dateFormat) {
		mDateFormat = dateFormat;
	}
	
	public SimpleDOMReader() {
	}
	
	@Override
	protected String getName(NodeList nl) throws Exception{
		if(nl == null || nl.getLength() != 1)
			throw new Exception();

		return nl.item(0).getTextContent();
	}

	@Override
	protected String getSymbol(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();
		
		return nl.item(0).getTextContent();
	}

	@Override
	protected float getMinRate(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();
		
		return Float.parseFloat(nl.item(0).getTextContent());
	}

	@Override
	protected float getMaxRate(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();

		return Float.parseFloat(nl.item(0).getTextContent());
	}

	@Override
	protected float getOpeningRate(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();

		return Float.parseFloat(nl.item(0).getTextContent());
	}

	@Override
	protected float getClosingRate(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();

		return Float.parseFloat(nl.item(0).getTextContent());
	}

	@Override
	protected float getCurrRate(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();

		return Float.parseFloat(nl.item(0).getTextContent());
	}

	@Override
	protected Date getDate(NodeList nl) throws Exception {
		if(nl == null || nl.getLength() != 1)
			throw new Exception();
		
		DateFormat formatter = new SimpleDateFormat(mDateFormat);
		return formatter.parse(nl.item(0).getTextContent());
	}
	
	private String mDateFormat = "yyy-mm-dd";
}
